package com.gzkit.demo.service.account;

import com.gzkit.baseframework.core.exceptions.BusinessException;
import com.gzkit.baseframework.core.utils.RequestMediaUtil;
import com.gzkit.baseframework.security.shiro.provider.AccountProvider;
import com.gzkit.baseframework.security.dto.UserAccount;
import com.gzkit.baseframework.sys.organization.service.SysOrganizationService;
import com.gzkit.baseframework.sys.organization.vo.SysOrganizationVO;
import com.gzkit.baseframework.sys.resource.entity.SysResource;
import com.gzkit.baseframework.sys.resource.service.SysResourceService;
import com.gzkit.baseframework.sys.role.vo.SysRoleVO;
import com.gzkit.baseframework.sys.user.service.SysUserBaseService;
import com.gzkit.baseframework.sys.user.service.SysUserService;
import com.gzkit.baseframework.sys.user.vo.SysUserBaseVO;
import com.gzkit.baseframework.sys.user.vo.SysUserDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by leodu
 * Created on 2018/7/10
 */
@Service
public class DefaultAccountService implements AccountProvider {

    @Autowired
    private SysUserBaseService userBaseService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysResourceService sysResourceService;

    @Autowired
    private SysOrganizationService sysOrganizationService;

    @Value("${mediaPath}")
    private String mediaPath;

    /**
     * {@link UserAccount}类中的extra字段，可以存放业务系统自定义的用户属性</br>
     * 用户登录后该字段中的内容可以通过jwt中payload取到</br>
     * @param name 用户名
     * @return 账户非敏感信息
     */
    @Override
    public UserAccount loadAccount(String name) {
        //根据用户名name查询系统用户信息
        SysUserBaseVO user = new SysUserBaseVO();
        user.setUsername(name);
        user = userBaseService.getOne(user);
        if (user != null) {
            //获取用户详情信息
            SysUserDetailVO detail = userBaseService.getDetailById(user.getId());
            UserAccount account = new UserAccount();
            account.setUsername(detail.getUsername());
            account.setPassword(detail.getPassword());
            account.setSalt(detail.getSalt());
            account.setStatus(detail.getUser().getStatus());
            account.setUid(detail.getId());
            account.setRealname(detail.getUser().getRealname());
            String headImg = RequestMediaUtil.appendUrl(mediaPath + "/headImg/" + detail.getId());
            Map<String, Object> extra = account.getExtra();
            extra.put("headImg", headImg);
            account.setExtra(extra);
            return account;
        }
        return null;
    }

    @Override
    public String loadPermissions(String username) {
        List<SysResource> list =sysResourceService.getUserResourceByUserName(username);
        if (null==list||list.isEmpty()){
            return "";
        }
        StringBuilder sb=new StringBuilder();
        for (SysResource sysResource: list){
            sb.append(sysResource.getPermissionCode());
            sb.append(",");
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }

    @Override
    public void lockAccount(String username) {
        //根据用户名name查询系统用户信息
        SysUserBaseVO user = new SysUserBaseVO();
        user.setUsername(username);
        user = userBaseService.getOne(user);
        if (user != null) {
            //锁定用户
            userService.lockAccount(user.getId());
        }else {
            throw new BusinessException("未查找到锁定用户");
        }
    }

    @Override
    public String loadRoles(String username) {
        List<SysRoleVO> list =sysResourceService.getUserRoleByUserName(username);
        if (null==list||list.isEmpty()){
            return "";
        }
        StringBuilder sb=new StringBuilder();
        for (SysRoleVO sysRoleVO: list){
            sb.append(sysRoleVO.getRoleCode());
            sb.append(",");
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }

    @Override
    public List<SysOrganizationVO> loadSysOrganizationByUserName(String username) {
        List<SysOrganizationVO> list = sysOrganizationService.loadSysOrganizationByUserNae(username);
        return list;
    }
}